package licode.unisop.auth.infrastructure.service;

import licode.unisop.oidc.auth.gateway.AuthorCache;
import licode.unisop.oidc.auth.stu.AuthorUtil;
import licode.unisop.provider.api.SiCacheProvider;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * 居于内存的缓存实现
 *
 * @author licode
 * @version 1.0
 */
@Component
public class SiCacheProviderMemory implements SiCacheProvider, ApplicationRunner {
    private AuthorCache authorCache;

    @PostConstruct
    public void init() {
        authorCache = AuthorUtil.getCache();
    }

    @Override
    public String getCacheType() {
        return "default";
    }

    @Override
    public String get(String key) {
        return authorCache.get(key);
    }

    @Override
    public void set(String key, String value, long timeout) {
        authorCache.set(key, value, timeout);
    }

    @Override
    public void update(String key, String value) {
        authorCache.update(key, value);
    }

    @Override
    public void delete(String key) {
        authorCache.delete(key);
    }

    @Override
    public long getTimeout(String key) {
        return authorCache.getTimeout(key);
    }

    @Override
    public void updateTimeout(String key, long timeout) {
        authorCache.updateTimeout(key, timeout);
    }

    @Override
    public Object getObject(String key) {
        return authorCache.getObject(key);
    }

    @Override
    public void setObject(String key, Object object, long timeout) {
        authorCache.setObject(key, object, timeout);
    }

    @Override
    public void updateObject(String key, Object object) {
        authorCache.updateObject(key, object);
    }

    @Override
    public void deleteObject(String key) {
        authorCache.deleteObject(key);
    }

    @Override
    public long getObjectTimeout(String key) {
        return authorCache.getObjectTimeout(key);
    }

    @Override
    public void updateObjectTimeout(String key, long timeout) {
        authorCache.updateObjectTimeout(key, timeout);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        this.init();
    }
}
